﻿using SuperMoney.Datas;
using SuperMoney.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperFx;
using OpenApi.OAuths;
using SuperMoney.Datas.DbServices;

namespace SuperMoney
{
	/// <summary>
	/// 每日赚服务实现。
	/// </summary>
	public class MeiRiMoneyService : IMeiRiMoneyService
	{
		/// <summary>
		/// 登陆。
		/// </summary>
		/// <param name="deviceId">设备编号。</param>
		/// <param name="consumerKey">消费者Key。</param>
		/// <returns></returns>
		public LoginResult Login(string deviceId, string consumerKey, string device)
		{
			var deviceAccount = DeviceAccountService.Instance.GetOrCreate(deviceId, device);
			var token = AccessTokenDbService.Instance.GetOrCreate(consumerKey, deviceId);
			LoginResult result = new LoginResult();
			result.DeviceId = deviceAccount.DeviceId;
			result.IntegralBalance = deviceAccount.IntegralBalance;
			result.Token = token.Token;
			result.Secret = token.Secret;
			return result;
		}

		/// <summary>
		/// 获取积分余额。
		/// </summary>
		/// <param name="deviceId">设备编号。</param>
		/// <returns></returns>
		public int GetIntegralBalance(string deviceId)
		{
			var deviceAccount = DeviceAccountDbService.Instance.Get(deviceId);
			if (deviceAccount == null) return 0;
			return deviceAccount.IntegralBalance;
		}

		/// <summary>
		/// 获取商品列表。
		/// </summary>
		/// <param name="count">每页条数。</param>
		/// <param name="page">当前页码。</param>
		/// <returns></returns>
		public List<Commodity> GetCommodities(int count, int page)
		{
			this.RedressCountAndPage(ref count, ref page);
			return CommodityDbService.Instance.GetList(count, page);
		}

		/// <summary>
		/// 兑换商品。
		/// </summary>
		/// <param name="deviceId">设备编号。</param>
		/// <param name="commodityId">商品编号。</param>
		/// <param name="number">兑换数量。</param>
		public void ExchangeCommodity(string deviceId, long commodityId, int number, string shipAddress)
		{
			Commodity commodity = CommodityDbService.Instance.Get(commodityId);
			#region 生成订单
			ConsumeOrder consumerOrder = new ConsumeOrder();
			consumerOrder.DeviceId = deviceId;
			consumerOrder.CommodityId = commodityId;
			consumerOrder.CommodityNumber = number;
			consumerOrder.ShipAddress = shipAddress;
			consumerOrder.Status = 0;
			// 计算所需积分金额
			consumerOrder.IntegralAmount = commodity.Integral * number;

			var deviceAccount = DeviceAccountService.Instance.Get(deviceId);
			if (deviceAccount.IntegralBalance < commodity.Integral)
			{
				throw new Exception("积分不足");
			}

			ConsumeOrderDbService.Instance.Create(consumerOrder);
			#endregion

			#region 支付订单
			IntegralTrade integralTrade = new IntegralTrade();
			integralTrade.DeviceId = deviceId;
			integralTrade.Category = 2;
			integralTrade.Name = String.Format("{0} ×{1}", commodity.Name, number);
			integralTrade.Integral = consumerOrder.IntegralAmount;
			integralTrade.Association = 2; // 关联商品
			integralTrade.AssociationId = consumerOrder.Id; // 商品编号

			IntegralTradeDbService.Instance.Create(integralTrade);
			DeviceAccountDbService.Instance.SubIntegralBalance(deviceId, integralTrade.Integral);
			#endregion

			#region 状态更新
			ConsumeOrderDbService.Instance.UpdateStatus(consumerOrder.Id, 1);
			#endregion
		}

		/// <summary>
		/// 获取任务记录。
		/// </summary>
		/// <param name="deviceId">设备编号。</param>
		/// <param name="count">每页条数。</param>
		/// <param name="page">当前页码。</param>
		/// <returns></returns>
		public List<TaskRecord> GetTaskRecords(string deviceId, int count, int page)
		{
			this.RedressCountAndPage(ref count, ref page);
			using (var db = MRMDB.New())
			{
				var adRecords = AdRecordDbService.Instance.GetList(deviceId, count, page);
				var taskRecords = adRecords.Convert2<TaskRecord>();
				return taskRecords;
			}
		}

		/// <summary>
		/// 获取兑换记录。
		/// </summary>
		/// <param name="deviceId">设备编号。</param>
		/// <param name="count">每页条数。</param>
		/// <param name="page">当前页码。</param>
		/// <returns></returns>
		public List<ExchangeRecord> GetExchangeRecords(string deviceId, int count, int page)
		{
			this.RedressCountAndPage(ref count, ref page);
			using (var db = MRMDB.New())
			{
				var consumeOrders = ConsumeOrderDbService.Instance.GetList(deviceId, count, page);
				var exchangeRecords = consumeOrders.Convert2<ExchangeRecord>();
				return exchangeRecords;
			}
		}

		/// <summary>
		/// 获取特定Key的全局设置项
		/// </summary>
		/// <param name="key">特定Key</param>
		/// <returns></returns>
		public GSetting GetGSetting(string key)
		{
			return GSettingDbService.Instance.GetGSetting(key);
		}

		private void RedressCountAndPage(ref int count, ref int page)
		{
			if (page < 1) page = 1;
			if (count < 1) count = 5;
		}
	}
}
